var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');

var isSend = 0;
//获取应用实例
var app = getApp();
var common = require('../../../utils/common.js')
Page({
   onLoad: function (options) {
     isSend = 0;
   },
   data: {
     mobile: '',
     name: '',
     avatarUrl: '',
     nickname: '',
     cardNo: '',
     msgCode: '',
     msgIntervalTime: 60,
     msgIntervalTimeLast: 60,
     sendMsgShow: true
   },
 
   formSubmit: function(e) {
    var that = this;
    var data = e.detail.value;
    if(data.kahao == ''){
        wx.showToast({
              title: '请输入会员卡号',	
			icon:'success', 
			duration: 2000, 
			image: '../../img/tips.png',
        });
       return false;
    }
    if(data.tel == ''){
        wx.showToast({
              title: '请输入手机号',	
			icon:'success', 
			duration: 2000, 
			image: '../../img/tips.png',
        });
       return false;
    }
    //匹配手机号格式
    if(!(/^1[345678]\d{9}$/.test(data.tel))){
       wx.showToast({  
            title: '请正确填写手机号',
            icon:'none', 
            duration: 2000  
        }); 
       return false;
     }
    if(data.code == ''){
        wx.showToast({
              title: '请输入验证码',
              icon: 'none',
              duration: 2000,
        });
       return false;
    }
    if(that.data.tel == undefined){
        wx.showToast({
              title: '手机号码前后不一致',	
			icon:'success', 
			duration: 2000, 
			image: '../../img/tips.png',
            });
          return false;
    }
    if(that.data.yangzhengma != data.code){
         wx.showToast({
              title: '验证码不正确',	
			icon:'success', 
			duration: 2000, 
			image: '../../img/tips.png',
            });
          return false;
    }
    if(that.data.tel != data.tel){
        wx.showToast({
              title: '手机号码前后不一致',	
			icon:'success', 
			duration: 2000, 
			image: '../../img/tips.png',
            });
          return false;
    }
    data.user_id = wx.getStorageSync('user_id');
    wx.request({
       url: app.globalData.base + '/Home/MyInfo/jihuoka',
        method: 'post',
        data: data,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success:function(res){
          if(res.data.status == '1'){
            wx.showToast({
              title: res.data.msg,
              duration: 2000,
              success:function(){
                wx.redirectTo({
                   url: '../../pages/memberCardlist/memberCardlist',
                })
              }
            });

          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            });
          }
        },
    });
  },




  getname: function (e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  getmobile: function (e) {
    var mobile = e.detail.value;
    this.setData({
      mobile: mobile
    });
    
  },
  getcardNo: function (e) {
    var cardNo = e.detail.value;
    this.setData({
      cardNo: cardNo
    })
  },
  getmsgCode: function (e) {
    var msgCode = e.detail.value;
    this.setData({
      msgCode: msgCode
    })
  },
  //获取验证码
  sendMsg: function () {
    if (isSend == 1) {
      return false;
    }
    isSend = 1;
    var that = this;
    var mobile = this.data.mobile;
    if (mobile == null || mobile == '') {
      util.showErrorToast('请输入手机号');
    }
    var data = {
      mobile: mobile
    }
    util.request(api.SendMsgCode, data, 'POST').then(function (data) {
      if (data.type === 'success') {
        util.showSuccessToast(data.content);
        isSend = 0;
        //屏蔽发送按钮+倒计时 
        that.setData({
          sendMsgShow: false
        })
        var msgIntervalTime = that.data.msgIntervalTime;
        var msgIntervalTimeLast = that.data.msgIntervalTimeLast;
        msgIntervalTimeLast = msgIntervalTime;
        that.setData({
          msgIntervalTimeLast: msgIntervalTimeLast
        });
        var interval = setInterval(function () {
          that.setData({
            msgIntervalTimeLast: msgIntervalTimeLast
          })
          msgIntervalTimeLast--;
          if (msgIntervalTimeLast == 0) {
            that.setData({
              sendMsgShow: true
            })
            clearInterval(interval);
          }
        }.bind(this), 1000);
      } else {
        util.showErrorToast(data.content);
        isSend = 0;
      }
    });
  },
  //提交信息
  tijiao: function (e) {
    var data = {
      cardNo: this.data.cardNo == null ? '' : this.data.cardNo,
      name: this.data.name == null ? '' : this.data.name,
      mobile: this.data.mobile == null ? '' : this.data.mobile,
      msgCode: this.data.msgCode == null ? '' : this.data.msgCode
    }
    util.request(api.ImproveInfoUrl, data, 'POST').then(function (data) {
      if (data.type === 'success') {
        util.showSuccessToast(data.content);
        setTimeout(function () {
          wx.navigateBack({ changed: true });//返回上一页
        }, 1500)
      } else {
        util.showErrorToast(data.content);
      }
    });
  }
  
});